<script setup lang="ts">
import FooterWidget from "@/components/landing/FooterWidget.vue";
import TopbarWidget from "@/components/landing/TopbarWidget.vue";
import { UserService } from "@/service/UserService";
import { onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { CommonService } from "../common";
// 尝试这种导入方式
import { useToast } from "primevue/usetoast";
import Toast from 'primevue/toast';

const route = useRoute();
const router = useRouter();
const currentUser = ref({
    name: "",
    sn: "",
    role: 0,
});
let toast = null;
try {
    toast = useToast();
} catch (e) {
    console.warn('PrimeVue Toast 服务不可用:', e.message);
}

// 错误处理函数
const handleError = (title) => {
    if (toast) {
        toast.add({ severity: "error", summary: "Error Message", detail: title, life: 3000 });
    } else {
        // 降级处理：使用console输出
        console.error('Toast不可用:', title);
    }
};
onMounted(() => {
    const snStr = route.params.sn + "";
    let AuthorizationStr = localStorage.getItem("Authorization");
    const payload = CommonService.parseJwtPayload(AuthorizationStr);
    if (!payload) {
        router.push({ name: "login" });
        return;
    }
    try {

        if (payload.sn == snStr) {
            currentUser.value = payload;
        } else {

            UserService.getUserInfo(snStr)
                .then((result) => {
                    if (result.Code != 1000) {
                        handleError(result.Title);
                        return;
                    } else {
                        if (result.Data.length == 0) {
                            handleError("查无此人");
                            return;
                        }
                        currentUser.value.name = result.Data.Name;
                        currentUser.value.sn = result.Data.Sn;
                        currentUser.value.role = result.Data.Role;
                        console.log("currentUser:", currentUser)
                    }
                })
                .catch((error) => {
                    handleError("查询用户出错");
                });
        }
    } catch (err) {
        console.log(err);

        return;
    }
});
const handleChannelEvent = (channel) => {
    switch (channel) {
        case "question":
            router.push({ path: "/client/" + route.params.sn + "/chatlist" });
            break;
        case "achievment":
            router.push({ path: "/client/" + route.params.sn + "/achievment" });
            break;
        case "info":
            router.push({ path: "/client/" + route.params.sn + "/profile" });
            break;
    }
};
</script>

<template>
    <div class="bg-surface-0 dark:bg-surface-900">
        <Toast />
        <div id="home" class="landing-wrapper overflow-hidden">
            <div class="py-6 px-6 mx-0 md:mx-12 lg:mx-20 lg:px-20 flex items-center justify-between relative lg:static">
                <TopbarWidget :currentClient="currentUser" @channel="handleChannelEvent" />
            </div>
            <router-view></router-view>
            <!-- <HeroWidget /> -->
            <!-- 1学习资料类、2生活用品类、3电子产品类、4衣物配饰类、5运动器材类、6乐器类、7家具类、8交通工具类、9美妆护肤品类、10装饰品类 -->
            <!-- <PricingWidget /> -->

            <!-- <FeaturesWidget kind="1" />
      <FeaturesWidget kind="2" />
      <FeaturesWidget kind="3" />
      <FeaturesWidget kind="4" />
      <FeaturesWidget kind="5" />
      <FeaturesWidget kind="6" />
      <FeaturesWidget kind="7" />
      <FeaturesWidget kind="8" />
      <FeaturesWidget kind="9" />
      <FeaturesWidget kind="10" /> -->
            <FooterWidget />
        </div>
    </div>
</template>